package com.fire.controller;


import com.fire.exception.ServiceException;
import com.fire.model.dto.MachineDto;
import com.fire.model.entity.Machine;
import com.fire.response.ResultData;
import com.fire.service.IMachineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-27
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequiredArgsConstructor
@Api(tags = {"机器管理模块"})
@RequestMapping("/machine")
public class MachineController {

    private final IMachineService machineService;

    /**
     * 新增机器接口
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增机器")
    public ResultData save(@RequestBody @Valid MachineDto dto) throws ServiceException {
        log.info("管理员开始新增机器，编号为：{}",dto.getMachineId());
        machineService.saveMachine(dto);
        log.info("管理员新增机器成功，编号为：{}",dto.getMachineId());
        return ResultData.success();
    }

    /**
     * 机器数据分页查询（带编号精确查询）
     * @param pageNo
     * @param pageSize
     * @param machineId
     * @return
     */
    @GetMapping("/query/{pageNo}/{pageSize}")
    @ApiOperation(value = "分页查询机器接口-带编号精确查询")
    public ResultData queryMatchMachineId(@PathVariable("pageNo") Integer pageNo,
                            @PathVariable("pageSize") Integer pageSize,
                            String machineId) throws ServiceException {
        log.info("机器数据分页查询：机器编号为：{}",machineId);
        Map<String,Object> queryMap =  machineService.queryMatchMachineId(pageNo,pageSize,machineId);
        log.info("机器数据分页查询成功，编号为：{}",machineId);
        return ResultData.success(queryMap);
    }

    /**
     * 机器数据分页查询（带位置模糊查询）
     * @param pageNo
     * @param pageSize
     * @param location
     * @return
     */
    @GetMapping("/query/like/{pageNo}/{pageSize}")
    @ApiOperation(value = "分页查询机器接口-带位置模糊查询")
    public ResultData queryMatchLocation(@PathVariable("pageNo") Integer pageNo,
                                          @PathVariable("pageSize") Integer pageSize,
                                          String location) throws ServiceException {
        log.info("机器数据分页查询：查询位置为：{}",location);
        Map<String,Object> queryMap =  machineService.queryMatchLocation(pageNo,pageSize,location);
        log.info("机器数据分页查询成功，查询位置为：{}",location);
        return ResultData.success(queryMap);
    }

    /**
     * 机器删除接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除机器")
    public ResultData delete(@PathVariable("id") Integer id) throws ServiceException {
        log.info("机器删除接口请求，删除机器的id为：{}",id);
        machineService.delete(id);
        log.info("机器删除接口请求成功，删除机器的id为：{}",id);
        return ResultData.success();
    }

    /**
     * 机器数据回显
     * @param id
     * @return
     */
    @GetMapping("/echo/{id}")
    @ApiOperation(value = "机器数据回显")
    public ResultData echo(@PathVariable("id") Integer id) throws ServiceException {
        log.info("机器数据回显请求，回显机器的id为：{}",id);
        MachineDto dto = machineService.echo(id);
        log.info("机器数据回显请求成功，回显机器的id为：{}",id);
        return ResultData.success(dto);
    }

    /**
     * 修改机器接口
     * @param dto
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改机器")
    public ResultData update(@RequestBody @Valid MachineDto dto) throws ServiceException {
        log.info("修改机器，编号为：{}",dto.getMachineId());
        machineService.updateMachine(dto);
        log.info("修改机器成功，编号为：{}",dto.getMachineId());
        return ResultData.success();
    }
}
